package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.listvm.BankListVm;
import com.typy.www.tpay.bean.vm.BankCardVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.adapter.BankCardAdapter;
import com.typy.www.tpay.ui.prt.PtrScrollContent;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/14.
 */

public class MyBankCardActivity extends BaseBackActivity {

    PtrScrollContent ptrScrollContent;
    private ListView mListView;
    private BankCardAdapter mAdapter;
    private List<BankCardVm> list = new ArrayList<>();
    private int classType;


    public static void show(Context context) {
        IntentUtil.intent2Activity(context, MyBankCardActivity.class);
    }

    public static void show(Context context, int classType) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        IntentUtil.intent2Activity(context, intent, MyBankCardActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("classType", 0);
        EventBus.getDefault().register(this);
        setTitle("我的银行卡");
        initAdapter();
        addAddNewLayout();
        getDate();
    }

    private void initAdapter() {
        mListView = findViewById(R.id.lv_root);
        mAdapter = new BankCardAdapter(getActivity());
        mAdapter.setClassType(classType);
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent msg) {
        if (msg.getKey().equals(MessageAction.REFRESHBANK)&&!isDestroy()) {
            getDate();
        } else if (msg.getKey().equals(MessageAction.DELBANK)) {
            getDel(msg.getMessage());
        } else if (msg.getKey().equals(MessageAction.DEFAULBANK)) {
            changeDefual(msg.getMessage(), msg.getMessage2());
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_list;
    }

    private void addAddNewLayout() {
        View addNiewCard = LayoutInflater.from(getActivity()).inflate(R.layout.item_addnewbank_line, null);
        addNiewCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddBankCardActivity.show(getActivity());
            }
        });
        mListView.addFooterView(addNiewCard);
    }

    private void getDate() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getBanklistURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret != null && ret.getData() != null) {
                    BankListVm vm = GsonUtils.parseJSON(ret.getData().toString(), BankListVm.class);
                    if (vm != null && vm.getData() != null) {
                        list = vm.getData();
                        mAdapter.setList(vm.getData());
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void getDel(String fdId) {
        ParamsUtil params = new ParamsUtil();
        params.put("fdId", fdId);
        HTTPUtils.post(getActivity(), Constance.getDelbankURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "删除成功", true);
                getDate();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void changeDefual(final String fdId, final String fdStatus) {
        ParamsUtil params = new ParamsUtil();
        params.put("fdId", fdId);
        params.put("fdStatus", fdStatus);
        HTTPUtils.post(getActivity(), Constance.getBankstausURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (classType == 0) {
                    if (fdStatus.equals("1")) {
                        UiHelper.showToast(getActivity(), "成功设为默认银行卡", true);
                    } else {
                        UiHelper.showToast(getActivity(), "成功取消默认银行卡", true);
                    }
                    refresh(fdId, fdStatus);
                } else
                    finish();
            }
            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    public void refresh(String fdId, String fdStatus) {
        for (BankCardVm vm : list) {
            if (vm.getFdId().equals(fdId)) {
                vm.setFdStatus(fdStatus);
            } else {
                vm.setFdStatus("0");
            }
        }
        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();
    }
}
